var str = "";
var arr = [];
//angular 模块开发
var app = angular.module("myApp",[]);

app.controller("myCtrl",function($scope){
	//用来判别搜索记录是否存在，是true否false
	$scope.show = true;
	//搜索的内容
	$scope.searchContent = "";
	//点击记录内容，跳到搜索结果页面
	$scope.arr = [];
	$scope.toSearch = function(){
		if($scope.searchContent != ""){
			if(window.localStorage.record){
				str = window.localStorage.record;
				if(str.indexOf(",") > 0){
                    $scope.arr = str.split(",");
				}else{
					$scope.arr[0] = str;
				}
				//用for循坏检测是否存在记录
				var a = false;
				for(var i=0; i<$scope.arr.length; i++){
					if($scope.arr[0] == $scope.searchContent){
						a = true;
					}
				}
				if(a == true){
					$scope.searchContent = encodeURI($scope.searchContent);
					$scope.searchContent = encodeURI($scope.searchContent);
					location.href = "searchResult.html?keyword=" + $scope.searchContent;
					return;
				}
				$scope.arr.unshift($scope.searchContent);
				window.localStorage.record = $scope.arr;
				//如果关键字是中文需要转义
				$scope.searchContent = encodeURI($scope.searchContent);
				$scope.searchContent = encodeURI($scope.searchContent);
				location.href = "searchResult.html?keyword=" + $scope.searchContent;
			}else{
				$scope.arr[0] = $scope.searchContent;
				window.localStorage.record = $scope.arr;
				location.href = "searchResult.html?keyword=" + $scope.searchContent;
			}
		}
	}
	
	//每次进来生成记录栏
	if(window.localStorage.record){
		$scope.show = true;
		str = window.localStorage.record;
		arr = str.split(",");
		if(arr.length >= 12){
			arr.length = 12;
		}
		for(var i=0; i<arr.length; i++){
			$scope.arr.push(arr[i]);
		}
	}else{
		$scope.show = false;
	}
	
	$scope.findThis = function(k){
		$scope.searchContent = k;
		$scope.searchContent = encodeURI($scope.searchContent);
		$scope.searchContent = encodeURI($scope.searchContent);
		location.href = "searchResult.html?keyword=" + $scope.searchContent;
	}
	
	$scope.removeRecord = function(){
		localStorage.clear();
		location.reload() ;
	}
	
})